%macro get_filing_status_nonfiler;

	
	proc printto log = "&logdir/get_filing_status_nonfiler.txt" new;
	run;
	
	
		data _null_;
			set rlib.nonfilers_prefs;
			file "&tmpdir./tinlist" dlm=',';
			put prim_tin; 
		run;


		/*
		Our task here is to:
		(1) identify whether the non-filer was a 2018 filer
		(2) identify if the non-filer was married in 2018, and to find prim_tin_x and prim_tin_y
		(3) if (1), identify if the spouse's TIN is a filer in 2019
		*/
		
		/****************************************
		*										*
		*		Were you married last year?		*
		*										*
		****************************************/
		proc sql noprint;
		[connection details redacted]
			create table prim as
			select * from connection to iq 
			(
			
			select
				a.prim_tin,
				b.[redacted] as prim_tin_x,
				case when b.[redacted] = 3 then 0 else b.[redacted] end as sec_tin_x,
				b.[redacted] as filing_status
			from #tinlist as a 
				inner join [redacted] as b
					on(a.prim_tin = b.[redacted])
			where b.[redacted] = &base_yr-1
		);
		/* 2019 primary is 2018 secondary */
		create table sec as 
		select * from connection to iq 
		(
		select 
			a.prim_tin,
			b.[redacted] as prim_tin_x,
			b.[redacted] as sec_tin_x,
			b.[redacted] as filing_status
		from #tinlist as a 
			inner join [redacted] as b 
				on(a.prim_tin = b.[redacted])
		where b.[redacted] = &base_yr-1
			and a.prim_tin > 0
			and b.[redacted] = 2
			);

	disconnect from iq;
	quit;
	
	/* all contains last year's filers */
	data all;
		set prim sec;
	run;
	proc sort data = all nodupkey; /* arbitrary dup drop */
		by prim_tin;
	run;
	proc sort data = rlib.nonfilers_prefs;
		by prim_tin;
	run;
	
	/* the single non-filers are those in rlib.nonfiler_prefs
		EXCEPT those who have filing_status = 2 in all */
	data single;
		merge rlib.nonfilers_prefs all(in=b);
		by prim_tin;
		if b = 0 | filing_status ~= 2;
		if b = 0 then filing_status = 1;
		nonfiler_x = 1-b;
	run;
	
	
	
	
	
	
	
	/********************************************
	*	Now, do the work on the married ones	*
	********************************************/
	data lag_married;
		set all(where=(sec_tin_x>0 & prim_tin = prim_tin_x));
	run;
	
	/* need to make sure last year's spouse is not a 2019 filer */
	proc sort data = lag_married nodupkey out = nodups;
		by sec_tin_x;
	run;
	
	
	
		/* 
		In this chunk, we find the set of sec_tin_x that are filers in 2019.
		We will drop them in the ensuing step.
		*/
		data _null_;
			set nodups;
			file "&tmpdir./tinlist" dlm=',';
			put sec_tin_x; 
		run;

		proc sql noprint;
		[connection details redacted]

		create table sample as
			select * from connection to iq 
			(
			
				/* If sec_tin_x is primary in 2019 */
				select distinct
					b.sec_tin_x
				into #prim
				from [redacted] as a
					inner join #tinlist as b
						on(a.[redacted] = b.sec_tin_x)
				where a.[redacted] = &base_yr 
					and (a.[redacted] ~= 1 or [redacted] ~= 1 or [redacted] ~= 1 or [redacted] <= 202015)
				
				
				
				/* If sec_tin_x is secondary in 2019 */
				
				select distinct 
					c.sec_tin_x
				into #sec
				from [redacted] as a
					inner join [redacted] as b
						on(a.[redacted] = b.[redacted] and a.[redacted] = b.[redacted])
					inner join #tinlist as c
						on(b.[redacted] = c.sec_tin_x)
				where a.[redacted] = &base_yr
					and a.[redacted] = 2
					and  (a.[redacted] ~= 1 or a.[redacted] ~= 1  or a.[redacted] ~= 1 or a.[redacted] <= 202015)
					and b.[redacted] > 0
					
				
					
				
				select 
					sec_tin_x
				from #prim
				union
				select 
					sec_tin_x
				from #sec
				
			
					
				
	);
	disconnect from iq;
	quit;
	
	/*
	Drop the people that should be dropped
	*/
	
	proc sort data = lag_married;
		by sec_tin_x;
	run;
	proc sort data = sample;
		by sec_tin_x;
	run;
	
	data married;
		merge lag_married(in=a) sample(in=b);
		by sec_tin_x;
		if a = 1 & b = 0;
	run;
	
	proc sort data = married;
		by prim_tin;
	run;
	data married;
		merge rlib.nonfilers_prefs(in=a) married(in=b);
		by prim_tin;
		if a & b;
		nonfiler_x = 0;
	run;
	
	
	
	/* append married and single */
	data rlib.nonfilers;
		set single married;
		sec_tin = sec_tin_x;
		if sec_tin = . then sec_tin = 0;
	run;
	
	
	
	





	
	proc printto;
	run;



%mend;